package com.lifo.shoppingcart.service.serviceimpl;

import com.lifo.shoppingcart.bean.HfShoppingCart;
import com.lifo.shoppingcart.dao.ShoppingCartManagerMapper;
import com.lifo.shoppingcart.service.ShoppingCartManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * Created by Administrator on 2018/6/25 0025.
 */
@Slf4j
@Service
public class ShoppingCartManagerServiceImpl implements ShoppingCartManagerService {

    @Autowired
    private ShoppingCartManagerMapper shoppingCartManagerDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void saveShoppingCart(HfShoppingCart hfShoppingCart) {
        try {
            shoppingCartManagerDao.saveShoppingCart(hfShoppingCart);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("用户id==>{},保存商品至购物车失败==>{}", hfShoppingCart.getUserId(), e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public List<HfShoppingCart> searchHfShoppingCartsByUserId(int userId) {
        List<HfShoppingCart> list = null;
        try {
            list = shoppingCartManagerDao.searchHfShoppingCartsByUserId(userId);
        } catch (Exception e) {
            log.error("用户id==>{},查询购物车商品失败==>{}", userId, e.getMessage());
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<HfShoppingCart> searchHfShoppingCartsByUserIdGroupByConditions(int userId) {
        List<HfShoppingCart> list = null;
        try {
            list = shoppingCartManagerDao.searchHfShoppingCartsByUserIdGroupByConditions(userId);
        } catch (Exception e) {
            log.error("用户id==>{},查询购物车商品失败==>{}", userId, e.getMessage());
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public HfShoppingCart searchHfShoppingCartByCartId(int cartId) {
        return shoppingCartManagerDao.searchHfShoppingCartByCartId(cartId);
    }

    @Override
    public int countByUserId(int userId) {
        return shoppingCartManagerDao.countByUserId(userId);
    }
}
